package com.core.family.biz.yizhao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.core.common.exception.BDException;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.family.biz.yizhao.domain.dto.YzUserDto;
import com.core.family.biz.yizhao.domain.po.YzOrder;
import com.core.family.biz.yizhao.mapper.YzCommentMapper;
import com.core.family.biz.yizhao.service.YzOrderService;
import com.core.family.biz.yizhao.service.YzUserExtendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.core.family.biz.yizhao.domain.po.YzComment;
import com.core.family.biz.yizhao.service.YzCommentService;
import com.core.common.gen.BaseServiceImpl;

import java.util.List;
import java.util.Objects;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-6-14 17:05:09
 * @Description 服务实现层 有一招评价表
 */
@Slf4j
@Service
public class YzCommentServiceImpl extends BaseServiceImpl<YzCommentMapper, YzComment> implements YzCommentService {

    @Autowired
    YzOrderService orderService;
    @Autowired
    YzUserExtendService extendU;


    @Override
    public IPage<YzComment> selectPage(YzComment yzComment, PageQuery<BaseQuery> pageQuery, boolean copyBean) {
        IPage<YzComment>  page= super.selectPage(yzComment, pageQuery, copyBean);
        for (YzComment record : page.getRecords()) {
            record.setSubList(getSubBy(record.getId()));
        }
        return page;
    }

    @Override
    public List<YzComment> selectList(YzComment yzComment, BaseQuery queryBean) {
        List<YzComment> list = super.selectList(yzComment, queryBean);
        for (YzComment comment : list) {
            comment.setSubList(getSubBy(comment.getId()));
        }
        return list;
    }

    @Override
    public List<YzComment> getSubBy(int parentId) {
        LambdaQueryWrapper<YzComment> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(YzComment::getParentId,parentId);
        return this.baseMapper.selectList(wrapper);
    }


    @Override
    public boolean saveOrUpdate(YzComment entity) {
        YzOrder order= orderService.getById(entity.getOrderId());
        if (Objects.isNull(order)){
            throw new BDException("对应订单不存在");
        }
        if (! "".equals(order.getStatus())){
          //  throw new BDException("非完成订单不允许评价");
        }
        YzUserDto userFr=extendU.getDetailById(entity.getOUserId());
        if (Objects.isNull(userFr)){
            throw new BDException("评价人id不正确，用户不存在");
        }
        Integer userIdTo=order.getOrderTakeUserId();
        if (Objects.isNull(userIdTo)){
            userIdTo=entity.getBUserId();
        }
        YzUserDto userTo=extendU.getDetailById(userIdTo);

        if (Objects.nonNull(userTo)){
            entity.setUnameTo(userTo.getUsername());
            entity.setUHeadimgTo(userTo.getAvatar());
        }

        entity.setBUserId(userIdTo);
        entity.setKindName(order.getKindName());
        entity.setOrderName(order.getProjectName());
        entity.setProductName(order.getProductName());
        entity.setPrice(order.getPrice());
        entity.setUserNameSt(userFr.getUsername());
        entity.setUserHeadImg(userFr.getAvatar());

        return super.saveOrUpdate(entity);
    }
}

